package com.youlai.system.service.impl.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.mapper.device.DeviceConfigMapper;
import com.youlai.system.mapper.device.DeviceMapper;
import com.youlai.system.pojo.entity.device.DeviceConfigEntity;
import com.youlai.system.pojo.entity.device.DeviceInfoEntity;
import com.youlai.system.pojo.query.DevicePageQuery;
import com.youlai.system.pojo.vo.device.DevicePageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.stream.Collectors;

@Component
public class DeviceService extends ServiceImpl<DeviceMapper, DeviceInfoEntity> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private DeviceConfigMapper deviceConfigMapper;

    public void deleteDeviceId(Integer deviceId){
        this.removeById(deviceId);

        LambdaUpdateWrapper<DeviceConfigEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DeviceConfigEntity::getDeviceId, deviceId);

        deviceConfigMapper.delete(lambdaUpdateWrapper);
    }

    public IPage<DevicePageVO> getDevicePage(DevicePageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<DeviceInfoEntity> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<DeviceInfoEntity> devicePage = this.baseMapper.getDevicePage(page, queryParams);

        IPage<DevicePageVO> devicePageVOIPage = new Page<>();
        devicePageVOIPage.setPages(devicePage.getPages());
        devicePageVOIPage.setTotal(devicePage.getTotal());

        devicePageVOIPage.setRecords(devicePage.getRecords()
                .stream().map(
                        deviceInfoEntity -> {
                            DevicePageVO devicePageVO = new DevicePageVO();
                            devicePageVO.setId(deviceInfoEntity.getId());
                            devicePageVO.setName(deviceInfoEntity.getName());
                            devicePageVO.setMac(deviceInfoEntity.getMac());
                            devicePageVO.setLng(deviceInfoEntity.getLng());
                            devicePageVO.setLat(deviceInfoEntity.getLat());
                            devicePageVO.setHardVersion(deviceInfoEntity.getHardVersion());
                            devicePageVO.setSoftVersion(deviceInfoEntity.getSoftVersion());
                            devicePageVO.setSimMobile(deviceInfoEntity.getSimMobile());
                            DeviceConfigEntity deviceConfigEntity = getByDeviceId(deviceInfoEntity.getId());
                            String height = "0";
                            if(deviceConfigEntity != null && StringUtils.isNotEmpty(deviceConfigEntity.getHeight())){
                                height = deviceConfigEntity.getHeight();

                            }
                            String jsonData = stringRedisTemplate.opsForValue().get("device.sensor." + deviceInfoEntity.getId());
                            if (StringUtils.isNotBlank(jsonData)) {
                                JSONObject json = JSON.parseObject(jsonData);
                                String yw = json.getJSONObject("sensor").getString("liquid_level");
                                devicePageVO.setCurrentYw(new BigDecimal(height).subtract(new BigDecimal(yw)).abs().toString());
                                devicePageVO.setTemp(json.getJSONObject("sensor").getString("temp_dat"));
                            }
                            Boolean online = new Integer(1).equals(deviceInfoEntity.getOnlineStatus());
                            devicePageVO.setOnline(online ? "在线" :"离线");
                            return devicePageVO;
                        }
                ).collect(Collectors.toList()));

        return devicePageVOIPage;
    }


    private DeviceConfigEntity getByDeviceId(Integer deviceId){
        LambdaQueryWrapper<DeviceConfigEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DeviceConfigEntity::getDeviceId, deviceId);

        return deviceConfigMapper.selectOne(lambdaQueryWrapper);
    }

}
